package oj;

import java.util.ArrayList;

public class Num501 {
    int pre=0;
    int curTimes=0;
    int maxTimes=0;
    ArrayList<Integer> list=new ArrayList<Integer>();
    public int[] findMode(TreeNode root) {
        inorder(root);
        int size=list.size();
        int[] arr=new int[size];
        for (int i = 0; i < size; i++) {
            arr[i]=list.get(i);
        }
        return arr;
    }

    public void inorder(TreeNode root) {
        if(root!=null) {
            inorder(root.left);
            if (pre == root.val) {
                curTimes++;
            }else {
                pre=root.val;
                curTimes=1;
            }
            if(curTimes==maxTimes){
                list.add(root.val );
            }else if(curTimes>maxTimes){
                list.clear();
                list.add(root.val);
                maxTimes=curTimes;
            }
            inorder(root.right);
        }
    }
}
